package com.spikegpsongoogleappengine;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;

public class RegularCompanyInfoOverlay extends
		ItemizedOverlay<RegularCompanyInfoItem> {

	private static final String TAG = "RegularCompanyInfoOverlay";
	private List<CompanyInfo> companyInfos = new ArrayList<CompanyInfo>();
	private Context mContext;

	public RegularCompanyInfoOverlay(Drawable defaultMarker, Context mContext) {
		super(defaultMarker);
		Log.d(TAG, "Creating " + TAG);
		setLastFocusedIndex(-1);
		populate();
		this.mContext = mContext;
	}

	@Override
	protected RegularCompanyInfoItem createItem(int i) {
		CompanyInfo companyInfo = companyInfos.get(i);
		int latitude = (int) (companyInfo.getLatitude() * 1E6D);
		if (latitude == 0) {
			Log.d(TAG, "Latitude is 0");
			return null;
		}

		int longitude = (int) (companyInfo.getLongitude() * 1E6D);
		if (longitude == 0) {
			Log.d(TAG, "Longitude is 0");
			return null;
		}

		GeoPoint p = new GeoPoint(latitude, longitude);
		Log.d(TAG, "Adding point lat: " + latitude + " lon: " + longitude);
		return new RegularCompanyInfoItem(p, companyInfo.getFnOrgUrl(),
				companyInfo.getStreetAddress());
	}

	protected int getIndexToDraw(int i) {
		// Log.d(TAG, "Calling getIndexToDraw with int: " + i);
		// Log.d(TAG, "The current size of the overlay is " +
		// companyInfos.size());
		int index = 0;
		try {
			index = super.getIndexToDraw(i);
		} catch (ArrayIndexOutOfBoundsException e) {
			Log.w(TAG, "", e);
		}
		return index;
	}

	@Override
	public int size() {
		Log.d(TAG, "Items size: " + companyInfos.size());
		return companyInfos.size();
	}

	public void setCompanyInfos(List<CompanyInfo> companyInfos) {
		this.companyInfos = companyInfos;
		populate();
	}

	@Override
	protected boolean onTap(int index) {
		CompanyInfo companyInfo = companyInfos.get(index);
		if (companyInfo != null) {
			AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
			dialog.setTitle(companyInfo.getFnOrgUrl());

			// SpannableString ss = new SpannableString(companyInfo.getTel());
			// ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
			// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			// "tel:" + companyInfo.getTel()
			// ss.setSpan(new URLSpan("tel:4155551212"), 0,
			// companyInfo.getTel().length(),
			// Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
			//
			// dialog.setMessage(companyInfo.getStreetAddress() + "\n"
			// + companyInfo.getLocality() + "\n" + ss);
			// dialog.
			dialog.setMessage("<a href=\"tel:4155551212\">dial a phone number</a>");
			dialog.show();

			 Intent intent = new Intent(mContext,
				 	SpecificCompanyInfoInteraction.class);
			mContext.startActivity(intent);

		} else {
			Log.w(TAG, "CompanyInfo is null");
		}
		return true;
	}

}